package com.sugon.controller.module;

import cn.hutool.core.date.DateUtil;
import com.sugon.annotation.AutoLog;
import com.sugon.annotation.NonBusinessParamValid;
import com.sugon.entity.MaintaPlanChangeRecordsEntity;
import com.sugon.entity.ModuleMaintainPlanEntity;
import com.sugon.modules.mold.model.vo.MoldMaintenancePlanDelayApplyParamVO;
import com.sugon.modules.mold.service.IMoldMaintenanceManager;
import com.sugon.service.MaintaPlanChangeRecordsService;
import com.sugon.service.ModuleMaintainPlanService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import com.sugon.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 维护计划变更记录Controller
 *
 * @author sugon
 * @date 2019-09-25 11:44:29
 */
@Controller
@RequestMapping("maintaplanchangerecords")
public class MaintaPlanChangeRecordsController {

    @Autowired
    private MaintaPlanChangeRecordsService maintaPlanChangeRecordsService;
    @Autowired
    private IMoldMaintenanceManager moldMaintenanceManager;
    @Autowired
    private ModuleMaintainPlanService moduleMaintainPlanService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("maintaplanchangerecords:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<MaintaPlanChangeRecordsEntity> maintaPlanChangeRecordsList = maintaPlanChangeRecordsService.queryList(query);
        int total = maintaPlanChangeRecordsService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(maintaPlanChangeRecordsList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("maintaplanchangerecords:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        MaintaPlanChangeRecordsEntity maintaPlanChangeRecords = maintaPlanChangeRecordsService.queryObject(id);

        return R.ok().put("maintaPlanChangeRecords", maintaPlanChangeRecords);
    }

    /**
     * 保存
     */
    @NonBusinessParamValid
    @AutoLog("维护计划变更记录--保存")
    @RequestMapping("/save")
    @ResponseBody
    public R save(@Validated @RequestBody MoldMaintenancePlanDelayApplyParamVO moldMaintenancePlanDelayApplyParam, BindingResult bindingResult) {
        // 1、校验工单是否存在以及是否完成
        ModuleMaintainPlanEntity plan = moduleMaintainPlanService.queryObject(moldMaintenancePlanDelayApplyParam.getTicketId());
        if (plan == null) {
            return R.error("模具定期维护计划不存在，请确认！");
        }
        // 2、申请时间不得早于截止时间
        String changeTime = moldMaintenancePlanDelayApplyParam.getChangeTime();
        if (plan.getPlanEndDate().after(DateUtil.parseDate(changeTime))) {
            return R.error("延期申请时间不得早于原计划截止时间！");
        }
        moldMaintenancePlanDelayApplyParam.setApplier(ShiroUtils.getUserId());
        moldMaintenancePlanDelayApplyParam.setModuleMaintainPlan(plan);
        return moldMaintenanceManager.applyChange(moldMaintenancePlanDelayApplyParam) ? R.ok() : R.error();
    }

    /**
     * 修改
     */
    @AutoLog("维护计划变更记录--修改")
    @RequestMapping("/update")
    @RequiresPermissions("maintaplanchangerecords:update")
    @ResponseBody
    public R update(@RequestBody MaintaPlanChangeRecordsEntity maintaPlanChangeRecords) {
        maintaPlanChangeRecordsService.update(maintaPlanChangeRecords);

        return R.ok();
    }

    /**
     * 删除
     */
    @AutoLog("维护计划变更记录--删除")
    @RequestMapping("/delete")
    @RequiresPermissions("maintaplanchangerecords:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        maintaPlanChangeRecordsService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<MaintaPlanChangeRecordsEntity> list = maintaPlanChangeRecordsService.queryList(params);

        return R.ok().put("list", list);
    }


    @RequestMapping("/isApply")
    @ResponseBody
    public R isApply(String id) {
        String i = maintaPlanChangeRecordsService.isApply(id);
        return R.ok().put("res", i);
    }


    @AutoLog("维护计划变更记录--修改状态")
    @RequestMapping("/updateApproval")
    @RequiresPermissions("maintaplanchangerecords:update")
    @ResponseBody
    public R updateApproval(@RequestBody MaintaPlanChangeRecordsEntity maintaPlanChangeRecords) {
        maintaPlanChangeRecordsService.updateApproval(maintaPlanChangeRecords);

        return R.ok();
    }

}
